package com.wkk.vo;

import com.wkk.constant.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
 /**
   *
   * @author :
   * @date : 2020/8/13 15:23
   * Description : 统一返回格式 用于展示层
   *
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class Result<T> implements Serializable {

      /** 序列化，反序列化验证标识 **/
     private static final long serialVersionUID = 2L;

      /** 请求成功标识  **/
     @ApiModelProperty(value = "请求成功标识")
     private boolean flag;

      /** 请求结果信息返回 **/
      @ApiModelProperty(value = "请求结果")
     private String message;

      /** 返回状态码 **/
      @ApiModelProperty(value = "返回状态码")
     private Integer code;

      /** 请求数据 **/
      @ApiModelProperty(value = "请求数据")
     private  T record;

      /** 时间戳 **/
      @ApiModelProperty(value = "时间戳")
     private long timestamp = System.currentTimeMillis();


     public Result<T> success(String message){
         this.flag = true;
         this.message = message;
         this.code = CommonConstant.RESULT_OK_200;
         return this;
     }


     public  Result<T> fail(String message){
         this.flag = false;
         this.message = message;
         this.code = CommonConstant.RESULT_ERROR_500;
         return this;
     }


     /* 以下均为 全局 静态方法 使用公共内存空间 且不会被释放一直占用 */

     /** 返回成功**/
     public static <T> Result<T> ok() {
         Result<T> result = new Result<>();
         result.setFlag(true);
         result.setCode(CommonConstant.RESULT_OK_200);
         result.setMessage("成功");
         return result;
     }

     /** 返回成功，自定义返回消息 **/
    /* public static <T> Result<T> ok(String msg) {
         Result<T> result = new Result<>();
         result.setFlag(true);
         result.setCode(CommonConstant.RESULT_OK_200);
         result.setMessage(msg);
         return result;
     }*/

     /** 返回成功，自定义数据 **/
     public static <T> Result<T> ok(T record) {
         Result<T> result = new Result<>();
         result.setFlag(true);
         result.setCode(CommonConstant.RESULT_OK_200);
         result.setMessage("成功");
         result.setRecord(record);
         return result;
     }

     /** 返回成功，自定义返回消息和数据 **/
     public static <T> Result<T> ok(String message , T record){
         Result<T> result = new Result<>();
         result.setCode(CommonConstant.RESULT_OK_200);
         result.setMessage(message);
         result.setRecord(record);
         result.setFlag(true);
         return result;
     }

     /** 返回错误，自定义信息 **/
     public static <T> Result<T> error(String message){
        Result<T> result = new Result<>();
        result.setMessage(message);
        result.setCode(CommonConstant.RESULT_ERROR_500);
        result.setFlag(false);
        return result;
     }

     /** 返回错误，自定义返回码和信息 **/
     public static <T> Result<T> error(Integer code,String message){
         Result<T> result = new Result<>();
         result.setCode(code);
         result.setMessage(message);
         result.setFlag(false);
         return result;
     }

     /** 无权限用户返回结果 **/
     public static <T> Result<T> noAuth(String message){
         return error(CommonConstant.RESULT_NO_AUTH_666,message);
     }

 }
